

#include <string.h>
#include <trace.h>
#include <wechats.h>
#include <gap.h>


T_SERVER_ID wechats_service_id;

static P_FUN_SERVER_GENERAL_CB pfn_wechats_service_cb = NULL; 

static uint8_t wechats_pedometer_value[4];
static uint8_t wechats_mac[6]; 

#define GATT_UUID_WECHAT_SERVICE        0xFEE7
#define GATT_UUID_CHAR_PEDO             0xFEA1
#define GATT_UUID_CHAR_TARGET           0xFEA2
#define GATT_UUID_CHAR_MAC              0xFEC9


const T_ATTRIB_APPL wechats_service_tbl[] = 
{
    /* << Primary Service >>, .. */
    {
        (ATTRIB_FLAG_VALUE_INCL | ATTRIB_FLAG_LE ), /*flags */
        {
            LO_WORD( GATT_UUID_PRIMARY_SERVICE ),
            HI_WORD( GATT_UUID_PRIMARY_SERVICE ),
            LO_WORD( GATT_UUID_WECHAT_SERVICE ),
            HI_WORD( GATT_UUID_WECHAT_SERVICE )
        },
        UUID_16BIT_SIZE,
        NULL,
        GATT_PERM_READ,
    },
    
    // PEDOMETER
    {
        ATTRIB_FLAG_VALUE_INCL,
        {
            LO_WORD( GATT_UUID_CHARACTERISTIC ),
            HI_WORD( GATT_UUID_CHARACTERISTIC ),
            GATT_CHAR_PROP_READ | GATT_CHAR_PROP_NOTIFY,
        },
        1,
        NULL,
        GATT_PERM_READ ,
    },
    {
        ATTRIB_FLAG_VALUE_APPL , // UUID
        {
           LO_WORD( GATT_UUID_CHAR_PEDO ),
           HI_WORD( GATT_UUID_CHAR_PEDO ),
        },
        0,
        NULL,
        GATT_PERM_READ 
    },
    {
        ATTRIB_FLAG_VALUE_INCL | ATTRIB_FLAG_CCCD_APPL, // cccd 
        {
            LO_WORD( GATT_UUID_CHAR_CLIENT_CONFIG ),
            HI_WORD( GATT_UUID_CHAR_CLIENT_CONFIG ),
            
            LO_WORD( GATT_CLIENT_CHAR_CONFIG_DEFAULT ),
            HI_WORD( GATT_CLIENT_CHAR_CONFIG_DEFAULT )
        },
        2,
        NULL,
        (GATT_PERM_READ | GATT_PERM_WRITE ),
    },
    
    // MAC
    {
        ATTRIB_FLAG_VALUE_INCL,
        {
            LO_WORD( GATT_UUID_CHARACTERISTIC ),
            HI_WORD( GATT_UUID_CHARACTERISTIC ),
            GATT_CHAR_PROP_READ ,
        },
        1,
        NULL,
        GATT_PERM_READ,
    },
    {
        ATTRIB_FLAG_VALUE_APPL , // UUID
        {
           LO_WORD( GATT_UUID_CHAR_MAC ),
           HI_WORD( GATT_UUID_CHAR_MAC ),
        },
        0,
        NULL,
        GATT_PERM_READ
    },

    
    
};

bool wechats_set_parameter(uint8_t param_type, uint16_t len, uint8_t* data)
{
    bool ret = true;
    
    switch( param_type )
    {
        default:
            ret =false;
        break;
        
        case WECHATS_SERVICE_PEDO_IDX:
            if( len == 4 ) 
            {
                memcpy( wechats_pedometer_value, data, 4 );  
				APP_PRINT_INFO0("[wechat] wechats_srv_id 3. ");
            }
            break;
        case WECHATS_SERVICE_CHAR_MAC_IDX:
            if( len == 6 )
            {
                memcpy( wechats_mac, data, 6 );
            }
            break;
    }
    
    return ret;
}


T_APP_RESULT wechats_svc_attr_read_cb(uint8_t conn_id, T_SERVER_ID service_id,
uint16_t attrib_index, uint16_t offset, uint16_t * p_length, uint8_t **pp_value )
{
    T_APP_RESULT cause = APP_RESULT_SUCCESS;

    switch( attrib_index )
    {
        default:
        cause = APP_RESULT_ATTR_NOT_FOUND;
        break;
        case WECHATS_SERVICE_PEDO_IDX:
        {
            T_WECHATS_CALLBACK_DATA callback_data;
            callback_data.msg_type = SERVICE_CALLBACK_TYPE_READ_CHAR_VALUE;
            callback_data.conn_id = conn_id;
            callback_data.msg_data.read_value_index = WECHATS_SERVICE_PEDO_IDX;
            if( pfn_wechats_service_cb )
            {
                pfn_wechats_service_cb( service_id, (void*)&callback_data );
            }
            *pp_value = wechats_pedometer_value;
            *p_length = 4;
        }
        break;
        case WECHATS_SERVICE_CHAR_MAC_IDX:
        {
            T_WECHATS_CALLBACK_DATA callback_data;
            callback_data.msg_type = SERVICE_CALLBACK_TYPE_READ_CHAR_VALUE;
            callback_data.conn_id = conn_id;
            callback_data.msg_data.read_value_index = WECHATS_SERVICE_CHAR_MAC_IDX;
            if( pfn_wechats_service_cb )
            {
                pfn_wechats_service_cb( service_id, (void*)&callback_data );
            }
            *pp_value = wechats_mac;
            *p_length = 6;
        }
        break;
    }
    return cause;
}

//T_APP_RESULT wechats_svc_attr_write_cb( uint8_t conn_id,
//                                    uint8_t service_id, 
//                                    uint16_t atrrib_index,
//                                    T_WRITE_TYPE write_type,
//                                    uint16_t length,
//                                    uint8_t *p_value,
//                                    P_FUN_WRITE_IND_POST_PROC * p_write_ind_post_proc
//)
//{
//    bool handle = true; 
//    
//    T_TTS_CALLBACK_DATA callback_data;
//    T_APP_RESULT cause = APP_RESULT_SUCCESS;
//    
//    switch( atrrib_index )
//    {
//        case WECHATS_SERVICE_PEDO_IDX:
//            callback_data.msg_type = SERVICE_CALLBACK_TYPE_WRITE_CHAR_VALUE;
//            callback_data.msg_data.write.write_type = WECHAT_PEDO_WRITE_VAL;
//            callback_data.msg_data.write.len = length;
//            callback_data.msg_data.write.p_value = p_value;
//            break;
//        // case TTS_SERVICE_CHAR_RX_WRITE_IDX:
//        //     callback_data.msg_type = SERVICE_CALLBACK_TYPE_WRITE_CHAR_VALUE;
//        //     callback_data.msg_data.write.write_type = TTS_WRITE_RX;
//        //     callback_data.msg_data.write.len = length;
//        //     callback_data.msg_data.write.p_value = p_value;
//        //     break;
//        default:
//            handle = false; 
//            cause = APP_RESULT_ATTR_NOT_FOUND;
//            break;
//    }
//    
//    if( ( pfn_wechats_service_cb ) && (handle == true ) )
//    {
//        pfn_tts_service_cb( service_id, (void*)&callback_data ); 
//    }
//    
//    return cause; 
//}

void wechats_svc_cccd_update_cb(
                            uint8_t conn_id,
                            T_SERVER_ID service_id,
                            uint16_t index, 
                            uint16_t cccd_bits)
{
    bool handle = true;
    
    T_WECHATS_CALLBACK_DATA callback_data;
    callback_data.msg_type = SERVICE_CALLBACK_TYPE_INDIFICATION_NOTIFICATION;
    
    switch( index ) 
    {
        case WECHATS_SERVICE_PEDO_CCCD_IDX:
            if( cccd_bits & GATT_CLIENT_CHAR_CONFIG_NOTIFY ) 
            {
                callback_data.msg_type = SERVICE_CALLBACK_TYPE_INDIFICATION_NOTIFICATION;
                callback_data.msg_data.notification_index = WECHATS_NOTIFY_PEDO_ENABLE;
            }
            else 
            {
                callback_data.msg_type = SERVICE_CALLBACK_TYPE_INDIFICATION_NOTIFICATION;
                callback_data.msg_data.notification_index = WECHATS_NOTIFY_PEDO_DISABLE;
            }
            break;        
        default:
            handle = false;
            break;
    }
    
    if( pfn_wechats_service_cb && (handle == true) ) 
    {
        pfn_wechats_service_cb( service_id, (void*)&callback_data ); 
    }    
}

bool wechats_send_pedo_data( uint8_t conn_id, T_SERVER_ID service_id, void *p_value, uint16_t length)
{
    return server_send_data( 
                            conn_id, 
                            service_id, 
                            WECHATS_SERVICE_PEDO_IDX, 
                            p_value, 
                            length, 
                            GATT_PDU_TYPE_ANY 
                            ); 
}


const T_FUN_GATT_SERVICE_CBS wechats_svc_cbs = 
{
    wechats_svc_attr_read_cb,
    NULL,
    wechats_svc_cccd_update_cb
};

uint8_t wechats_svc_add_service(void *p_func)
{
    uint8_t service_id;
    if( false == server_add_service( &service_id,
                                    (uint8_t*)wechats_service_tbl,
                                    sizeof( wechats_service_tbl ),
                                    wechats_svc_cbs) 
    )
    {
        service_id = 0xff;
        return service_id;
    }
    
    pfn_wechats_service_cb = (P_FUN_SERVER_GENERAL_CB)p_func;
    return service_id; 
}

